package UIComponents// ActionScript file
{
	import MyComponent.MyButton;
	
	import UIComponents.IconContainer;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	/**
	 *物品框 
	 * @author 葛增文
	 * 
	 */	
	public class FrameItem extends Sprite
	{
		private var _item:IUseItemWithPrice;/*展示对象*/
		public function FrameItem():void
		{
			this.init();
		}
		
		private function init():void
		{
			(this.getChildByName("txt_price") as TextField).mouseEnabled=false;
			this.addListener();
		}
		
		/**
		 *设定展示的道具对象 
		 * @param item
		 * 
		 */		
		public function setItem(item:IUseItemWithPrice):void
		{
			this.reset();
			this._item=item;
			if(this.item)
			{
			    this.setItemName(item.itemName);
			    this.setItemPic(item.itemPic);
			    this.setItemPriceType(item.itemPriceType);
			    this.setItemPrice(item.itemPrice);
			}
		}
		
		private function addListener():void
		{
			(this.getChildByName("button_price") as MyButton).addEventListener(MouseEvent.CLICK,clickPriceHandler);
		}
		
		private function removeListener():void
		{
			(this.getChildByName("button_price") as MyButton).removeEventListener(MouseEvent.CLICK,clickPriceHandler);
		}
		
		private function clickPriceHandler(e:Event):void
		{
			this.dispatchEvent(new PanelEvent("clickItemPriceHandler",true));
		}
		
		public function setItemName(value:String):void
		{
			(this.getChildByName("txt_name") as TextField).text=value;
		}
		
		public function setItemPic(value:DisplayObject):void
		{
			var tips:String=this.item.itemName+"："+this.item.description;
			((this.getChildByName("frame_icon") as Sprite).getChildByName("iconContainer") as IconContainer).addPic(value,tips);
		}
		
		public function setItemPriceType(value:Boolean):void
		{
			(this.getChildByName("icon_coin") as IconCoin).showType(value);
		}
		
		public function setItemPrice(value:Number):void
		{
			(this.getChildByName("txt_price") as TextField).text=String(value);
		}
		
		private function reset():void
		{
			this.setItemName("");
			this.setItemPic(null);
			this.setItemPriceType(true);
			this.setItemPrice(0);
		}
		
		public function release():void
		{
			this._item=null;
			this.removeListener();
			while(this.parent)this.parent.removeChild(this);
		}

		/*getter & setter*/
		public function get item():IUseItemWithPrice
		{
			return _item;
		}

	}
}